#include "StdAfx.h"
#include "CResousContener.h"
#include "SNRander.h"
#include "SNPublicFuc.h"

CResousContener* CResousContener::m_pResContener = NULL;

CResousContener::CResousContener(void)
{
	m_sRootPag = CPublicFuc::GetExePathName();
	int liPos = m_sRootPag.ReverseFind(_T('\\'));
	m_sRootPag = m_sRootPag.Left(liPos);
	m_sRootPag += _T("\\Picture\\");
}

CResousContener::~CResousContener(void)
{
	std::map<CString, HBITMAP>::iterator liIter;
	for (liIter = m_oIcoBitmap.begin(); liIter != m_oIcoBitmap.end(); ++liIter)
	{
		::DeleteObject(liIter->second);
	}
	m_oIcoBitmap.clear();

	for (liIter = m_pBitmapPagMap.begin(); liIter != m_pBitmapPagMap.end(); ++liIter)
	{
		::DeleteObject(liIter->second);
	}
	m_pBitmapPagMap.clear();
}

CResousContener* CResousContener::GetResContener(void)
{
	if (NULL == m_pResContener)
		m_pResContener = new CResousContener();
	return m_pResContener;
}

void CResousContener::ReleaseInstance()
{
	if (m_pResContener)
	{
		delete m_pResContener;
		m_pResContener = NULL;
	}
}


HBITMAP CResousContener::GetIcoBitmap(HDC ahHdc, CString asFileExtern)
{
	if (m_oIcoBitmap.find(asFileExtern) == m_oIcoBitmap.end())
	{
		SHFILEINFO lIcoInfo;   
		if (SHGetFileInfo(asFileExtern, 0, &lIcoInfo, sizeof(lIcoInfo), SHGFI_ICON|SHGFI_LARGEICON|SHGFI_USEFILEATTRIBUTES))
		{
			HBITMAP lhIcoBitmap = CSNRander::ConvertIconToBitmap(ahHdc, lIcoInfo.hIcon);
			::DestroyIcon(lIcoInfo.hIcon);
			m_oIcoBitmap.insert(std::make_pair(asFileExtern , lhIcoBitmap ));
			return lhIcoBitmap;
		}
	}
	else
	{
		return m_oIcoBitmap.find(asFileExtern)->second;
	}
	return NULL;
}


HBITMAP CResousContener::GetBitmapByPathName(CString asPathName)
{
	HBITMAP lhBmp = NULL;
	std::map<CString, HBITMAP>::iterator liIter;
	asPathName = m_sRootPag+asPathName;
	liIter = m_pBitmapPagMap.find(asPathName);
	if (liIter != m_pBitmapPagMap.end())
	{
		lhBmp = liIter->second;
	}
	else
	{
		lhBmp=(HBITMAP)::LoadImage(AfxGetInstanceHandle(), asPathName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE);  
		m_pBitmapPagMap.insert(std::make_pair(asPathName, lhBmp));
	}
	return lhBmp;
}